﻿/*
 * Created by SharpDevelop.
 * User: user
 * Date: 20/12/2009
 * Time: 19:35
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;

namespace csharp.rad.general
{
	/// <summary>
	/// Description of Supplier.
	/// </summary>
	public class Supplier : csharp.common.DefaultClass, csharp.common.IClass
	{
		public Person person = new Person();
		
		public Supplier()
		{
			gsql.table = "common.supplier";
			_sequence  = "common.supplier_supplier_id_seq";
		}
		
		public void clear()
		{
			_id = String.Empty;
			person.clear();
		}
		
		
		public override void get(System.Data.DataRow dr)
		{
			_id = dr["supplier_id"].ToString();
			person.id = dr["supplier_person_id"].ToString();
			person.get();
		}
		
		public bool getByPersonId(String person_id)
		{
			gsql.prepare();
			gsql.commandType = common.GenerateSql.TCommandType.CMD_SELECT;
			gsql.sql= "SELECT * "
						 +"FROM common.supplier "
						 +"JOIN common.person ON supplier_person_id = person_id "
						 +"WHERE person_company_id = "+Information.companyId
			             +" AND supplier_person_id ="+person_id;
			Int32 tam;
			
			if(!gsql.executeSql())
				return false;
			
			tam = gsql.getRowCount();
			if(tam == 1)
				this.get(gsql.getRow(0));
			return true;
		}		
		
		public override void setGsql()
		{
			gsql.setValue(_id, "supplier_id");
			gsql.setValue(person.id, "supplier_person_id");
		}
	}
	
	public class Suppliers : csharp.common.DefaultList, csharp.common.IList
	{
		public Suppliers()
		{
			
		}
		
		public Supplier get()
		{
			return (Supplier)items[index];
		}
		
		public Supplier get(Int32 index)
		{
			return (Supplier)items[index];
		}
		
		public bool fillList()
		{
			gsql.prepare();
			gsql.sql= "SELECT * "
						 +"FROM common.supplier "
						 +"JOIN common.person ON supplier_person_id = person_id "
						 +"WHERE person_company_id = "+Information.companyId;
			return mountList();
		}
		
		
		private bool mountList()
		{
			Int32 tam;
			
			if(!gsql.executeSql())
				return false;
			
			items.Clear();
			tam = gsql.getRowCount();
			for(int i=0; i < tam; i++)
			{
				Supplier s = new Supplier();
				s.get(gsql.getRow(i));
				items.Add(s);
			}
			return true;
		}
	
		public bool searchByCode(string code)
		{
			//items.Sort(
			int tam = items.Count;
			for(int i = 0; i < tam; i++)
			{
				if (((Supplier)items[i]).person.code.Equals(code))
				{
					index = i;
					return true;
				}
			}
			return false;			
		}
	}
}
